MyBatis框架(14) —— 缓存机制(基于注解)

目录结构

src

  • main
    • java
      • cn.water.dao
        • UserDao.java(持久层接口)
      • cn.water.domain
        • User.java(实体类)
    • resources
      • SqlMapConfig.xml(MyBatis主配置文件)
      • jdbcConfig.properties(数据库连接信息文件)
  • test
    • java.cn.water
      • MybatisTest.java(测试类)

MyBatis配置文件

jdbcConfig.properties

1
2
3
4
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=root

SqlMapConfig.xml

  • 开启 二级缓存
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">


<!-- mybatis的主配置文件 -->
<configuration>

<!-- 外部配置 -->
<properties resource="jdbcConfig.properties"></properties>

<!-- 配置参数 -->
<settings>
<!-- 开启 二级缓存 -->
<setting name="cacheEnabled" value="true"/>
</settings>

<!-- 指定包:实体类-->
<typeAliases>
<package name="cn.water.domain"/>
</typeAliases>

<!-- 配置环境 -->
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<!-- 配置连接数据库的4个基本信息 -->
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>

<!-- 指定包:持久层接口 -->
<mappers>
<package name="cn.water.dao"/>
</mappers>

</configuration>

实体类

User.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package cn.water.domain;

import java.io.Serializable;
import java.util.Date;

/**
* @author Water
* @date 2019/10/12 - 7:51
* @description
*/
public class User implements Serializable {

private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;

@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public Date getBirthday() {
return birthday;
}

public void setBirthday(Date birthday) {
this.birthday = birthday;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}
}

持久层接口

UserDao.java

  • 注解支持:@CacheNamespace(blocking = true)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package cn.water.dao;

import cn.water.domain.User;

/**
* @author Water
* @date 2019/10/12 - 7:51
* @description
*/
@CacheNamespace(blocking = true)
public interface UserDao {

@Select("SELECT * FROM user WHERE id = #{userId}")
User findById(Integer userId);

}

测试类

MyBatisT

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package cn.water;

import cn.water.dao.UserDao;
import cn.water.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

/**
* @author Water
* @date 2019/10/12 - 7:56
* @description
*/
public class MybatisTest {


/* 成员变量 */
private InputStream inputStream;
private SqlSessionFactory factory;
private SqlSession session;

/* 初始化操作 */
@Before
public void init() throws IOException {
/* 加载 MyBatis配置文件 */
inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
/* 获取 工厂类 */
factory = new SqlSessionFactoryBuilder().build(inputStream);
/* 获取 产品类 */
session = factory.openSession(true);/* 设置自动提交 */
}

/* 销毁操作 */
@After
public void destroy() throws IOException {
session.close();
inputStream.close();
}

/** 一级缓存:同一个 SqlSession */
@Test
public void test01(){
UserDao dao = session.getMapper(UserDao.class);
User user01 = dao.findById(41);
System.out.println("第一次查询:"+user01);
User user02 = dao.findById(41);
System.out.println("第二次查询:"+user02);
System.out.println("两次查询结果是否相等:"+(user01==user02)); //true
}

/** 一级缓存:不同 SqlSession */
@Test
public void test02(){
UserDao dao = session.getMapper(UserDao.class);
User user01 = dao.findById(41);
System.out.println("第一次查询:"+user01);

/* commit()(执行插入、更新、删除)、close() */
session.close();
session = factory.openSession();

UserDao dao02 = session.getMapper(UserDao.class);
User user02 = dao02.findById(41);
System.out.println("第二次查询:"+user02);
System.out.println("两次查询结果是否相等:"+(user01==user02));//false
}


}

开启二级缓存(缓存)

  • 二级缓存的使用步骤:
    • 第一步:让Mybatis框架支持二级缓存(在SqlMapConfig.xml中配置)
    • 第二步:让当前的持久层接口支持二级缓存(在UserDao.java中配置)
-------------本文结束-------------
Donate comment here